<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css" rel="stylesheet">
    <title>行程记录与查询</title>
</head>

<body class="bg-gray-100 font-sans">
    <div class="container mx-auto p-8">
        <h1 id="date-title" class="text-3xl font-bold text-center text-gray-800 mb-8">行程记录与查询</h1>
        <div class="bg-white p-6 rounded-lg shadow-md">
            <div class="mb-4">
                <label for="date" class="block text-gray-700 text-sm font-bold mb-2">选择日期</label>
                <input type="date" id="date" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
            </div>
            <button id="queryButton" class="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">
                <i class="fa-solid fa-magnifying-glass"></i> 查询行程
            </button>
        </div>
        <div class="bg-white p-6 rounded-lg shadow-md mt-8">
            <form id="itineraryForm">
                <div class="mb-4">
                    <label for="time" class="block text-gray-700 text-sm font-bold mb-2">时间</label>
                    <input type="time" id="time" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" required>
                </div>
                <div class="mb-4">
                    <label for="activity" class="block text-gray-700 text-sm font-bold mb-2">活动</label>
                    <input type="text" id="activity" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" placeholder="请输入活动内容" required>
                </div>
                <button type="submit" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">
                    <i class="fa-solid fa-plus"></i> 添加行程
                </button>
            </form>
        </div>
        <div class="mt-8">
            <h2 class="text-2xl font-bold text-gray-800 mb-4">行程列表</h2>
            <ul id="itineraryList" class="divide-y divide-gray-200"></ul>
        </div>
    </div>
    <script>
        const dateTitle = document.getElementById('date-title');
        const currentDate = new Date();
        const year = currentDate.getFullYear();
        const month = String(currentDate.getMonth() + 1).padStart(2, '0');
        const day = String(currentDate.getDate()).padStart(2, '0');
        const today = `${year}-${month}-${day}`;
        document.getElementById('date').value = today;
        dateTitle.textContent = `行程记录与查询 - ${year}年${month}月${day}日`;

        const itineraryForm = document.getElementById('itineraryForm');
        const itineraryList = document.getElementById('itineraryList');
        const dateInput = document.getElementById('date');
        const queryButton = document.getElementById('queryButton');

        function saveItinerary(date, time, activity) {
            let itineraries = JSON.parse(localStorage.getItem(date)) || [];
            itineraries.push({ time, activity });
            localStorage.setItem(date, JSON.stringify(itineraries));
        }

        function displayItineraries(date) {
            itineraryList.innerHTML = '';
            const itineraries = JSON.parse(localStorage.getItem(date)) || [];
            itineraries.forEach(item => {
                const listItem = document.createElement('li');
                listItem.classList.add('p-4', 'flex', 'items-center', 'justify-between', 'hover:bg-gray-100', 'transition', 'duration-300');
                listItem.innerHTML = `
                    <span class="text-gray-700">${item.time}: ${item.activity}</span>
                    <button class="text-red-500 hover:text-red-700 focus:outline-none">
                        <i class="fa-solid fa-trash"></i> 删除
                    </button>
                `;

                const deleteButton = listItem.querySelector('button');
                deleteButton.addEventListener('click', function () {
                    const index = itineraries.indexOf(item);
                    if (index > -1) {
                        itineraries.splice(index, 1);
                        localStorage.setItem(date, JSON.stringify(itineraries));
                        listItem.remove();
                    }
                });

                itineraryList.appendChild(listItem);
            });
        }

        itineraryForm.addEventListener('submit', function (e) {
            e.preventDefault();
            const time = document.getElementById('time').value;
            const activity = document.getElementById('activity').value;
            const selectedDate = dateInput.value;
            saveItinerary(selectedDate, time, activity);
            displayItineraries(selectedDate);
            itineraryForm.reset();
        });

        queryButton.addEventListener('click', function () {
            const selectedDate = dateInput.value;
            const year = new Date(selectedDate).getFullYear();
            const month = String(new Date(selectedDate).getMonth() + 1).padStart(2, '0');
            const day = String(new Date(selectedDate).getDate()).padStart(2, '0');
            dateTitle.textContent = `行程记录与查询 - ${year}年${month}月${day}日`;
            displayItineraries(selectedDate);
        });

        displayItineraries(today);
    </script>
</body>

</html>
    
